import { NextResponse } from 'next/server';
import prisma from '@/lib/prisma';
import { getUser } from '@/lib/authUtils';
export const dynamic = 'force-dynamic';
export async function GET(request: Request): Promise<Response> {
  try {
    const userid = await getUser(request);
    if (!userid || userid instanceof NextResponse) {
      return new Response(
        JSON.stringify({ error: '未授权或登录信息过期，请登录' }),
        { status: 401, headers: { 'Content-Type': 'application/json' } }
      );
    }
    const subtitles = await prisma.vms_asr.findMany({
      where: {
        userid: userid
      },
      orderBy: {
        create_time: 'desc',
      },
    });

    // Convert BigInt to string to prevent serialization errors
    const serializedSubtitles = subtitles.map(subtitle => ({
      ...subtitle,
      id: subtitle.id.toString(),
      task_id: subtitle.task_id?.toString()
    }));

    return new Response(JSON.stringify(serializedSubtitles), {
      headers: { 'Content-Type': 'application/json' }
    });
  } catch (error) {
    console.error('Failed to fetch subtitles:', error);
    return new Response(JSON.stringify({ error: 'Failed to fetch subtitles' }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' }
    });
  }
}
